//
//  PubCityViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "PubCityViewController.h"
#import "JourneyModel.h"
#import "JourneyTableViewCell.h"
#import "journeyCollectionTableCell.h"
#import "PubViewController.h"

@interface PubCityViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *journeyArr;
@property (nonatomic, strong) NSArray *arr;
@property (nonatomic, strong) NSMutableArray *hotArr;
@property (nonatomic, strong) NSMutableArray *otherArr;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation PubCityViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    }
    return self;
}

#pragma mark 旅途中数据解析
- (void)createData {
    NSString *strUrl = @"http://open.qyer.com/qyer/hotel/hot_city_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=199&page=1&track_app_channel=App%2520Store&track_app_version=6.6&track_device_info=iPad%2520Air%2520%28Wifi%29&track_deviceid=A24310D6-4784-474E-86CE-81D543A1EB8A&track_os=ios%25208.1.3&v=1";
    [ZSMAFNetWork geturl:strUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *strDic= result;
        NSMutableArray *strArr = strDic[@"data"];
        self.journeyArr = [NSMutableArray array];
        for (NSMutableDictionary *dic in strArr) {
            JourneyModel *journeyModel = [[JourneyModel alloc] init];
            [journeyModel setValuesForKeysWithDictionary:dic];
            [self.journeyArr addObject:journeyModel];
        }
        
        
        self.hotArr = [NSMutableArray array];
        for (NSInteger i = 0; i<9; i++) {
            JourneyModel *journeyModel = self.journeyArr[i];
            [self.hotArr addObject:journeyModel];
        }
        
        self.otherArr = [NSMutableArray array];
        // 遍历字母
        for (NSInteger i = 0; i<26; i++) {
            NSString *str = MISSSYMBOL[i];
            // 装对应字母的数组
            NSMutableArray *letterArr = [NSMutableArray array];
            for (NSInteger j = 0; j<self.journeyArr.count; j++) {
                JourneyModel *journey = self.journeyArr[j];
                // 首字母
                NSString *firstStr = [journey.pinyin substringToIndex:1];
                if ([firstStr isEqualToString:str]) {
                    [letterArr addObject:journey];
                }
            }
            [self.otherArr addObject:letterArr];
        }
        
        [self.tableView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        NSLog(@"错误是:%@",error);
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.text = @"选择城市";
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    // 左上角返回图标
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction:)];
    [self createTableView];
}

// 返回
- (void)leftAction:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableView的创建
- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 554)style:UITableViewStylePlain];
    self.tableView.dataSource = self ;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.hud hide:YES afterDelay:2];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);        
    }];
}

#pragma mark tableView的数据处理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 27;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else {
        NSArray *arr = self.otherArr[section - 1];
        return arr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 500 * HEIGHT;
    }
    else {
        return 80 * HEIGHT;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"热门";
    }
    else {
        return SYMBOL[section - 1];
    }
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:@"热门"];
    for (NSString *temp in SYMBOL) {
        [arr addObject:temp];
    }
    return arr;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *reuse = @"reuseOne";
        journeyCollectionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[journeyCollectionTableCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:reuse];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.arr = self.hotArr;
        [cell.journeyCollectionView reloadData];
        
        // block传值push下一页
        void(^block)(NSDictionary *) = ^(NSDictionary *dic){
            NSArray *arr = @[dic[@"id"],dic[@"photo"],dic[@"name"]];
            self.blockOnce(arr);
            [self.navigationController popViewControllerAnimated:YES];
        };
        cell.block = block;

        
        return cell;
    }
    else {
        static NSString *reuseTwo = @"reuseTwo";
        JourneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseTwo];
        if (!cell) {
            cell = [[JourneyTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:reuseTwo];
            cell.backgroundColor = [UIColor clearColor];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        JourneyModel *journeyModel = self.otherArr[indexPath.section - 1][indexPath.row];
        
        cell.titleLabel.text = journeyModel.cnname;
        cell.downLabel.text = journeyModel.pinyin;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section != 0) {
        JourneyModel *journeyModel = self.otherArr[indexPath.section - 1][indexPath.row];
        NSString *strId = [NSString stringWithFormat:@"%@",journeyModel.mId];
        NSArray *arr = @[strId,journeyModel.photo,journeyModel.cnname];
        self.blockOnce(arr);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
